/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.state;


import me.panpf.sketch.SketchView;
import me.panpf.sketch.decode.NotFoundGifLibraryException;
import me.panpf.sketch.drawable.MiPixmapElement;
import me.panpf.sketch.drawable.SketchLoadingDrawable;
import me.panpf.sketch.drawable.SketchShapeBitmapDrawable;
import me.panpf.sketch.request.DisplayOptions;
import me.panpf.sketch.request.ShapeSize;
import me.panpf.sketch.shaper.ImageShaper;
import me.panpf.sketch.util.SketchUtils;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.media.image.Image;

/**
 * 使用当前 {@link Image} 正在显示的图片作为状态图片
 */
public class OldStateImage implements StateImage {

    private StateImage whenEmptyImage;

    public OldStateImage(StateImage whenEmptyImage) {
        this.whenEmptyImage = whenEmptyImage;
    }

    public OldStateImage() {
    }

    @Override
    public Element getDrawable(Context context, SketchView sketchView, DisplayOptions displayOptions) {
        Element drawable;
        if (sketchView.getPixmapBuild() != null) {
            if (sketchView.getPixmapBuild().getImageSource() != null) {
                drawable = (PixelMapElement) SketchUtils.getLastDrawable(sketchView.getPixmapBuild().getImageSource());
            } else {
                drawable = new MiPixmapElement(sketchView.getPixmapBuild());
            }
        } else {
            drawable = sketchView.getDrawable();
        }

//        if (drawable instanceof SketchLoadingDrawable) {
//            drawable = ((SketchLoadingDrawable) drawable).getWrappedDrawable();
//        }

        if (drawable != null) {
            ShapeSize shapeSize = displayOptions.getShapeSize();
            ImageShaper imageShaper = displayOptions.getShaper();
            if (shapeSize != null || imageShaper != null) {
                if (drawable instanceof SketchShapeBitmapDrawable) {
                    drawable = new SketchShapeBitmapDrawable(context, ((SketchShapeBitmapDrawable) drawable).getBitmapDrawable(), shapeSize);
                } else if (drawable instanceof MiPixmapElement) {
                    drawable = new SketchShapeBitmapDrawable(context, (MiPixmapElement) drawable, shapeSize, imageShaper);
                }
            }
        }

        if (drawable == null && whenEmptyImage != null) {
            drawable = whenEmptyImage.getDrawable(context, sketchView, displayOptions);
        }

        return drawable;
    }
}
